'use strict'

define(
		[ 'jquery', 'bootstrap', 'select2', 'validate', 'validate.en',
				'koala-search', 'shiftform', 'modalSearch', 'datetimepicker',
				'datetimepickerCN', 'ajaxTab' ],
		function($, validate) {

			return {

				init : function(data) {
					this.initPage();
					this.initEmployee();
					this.submit(data);
				},

				initPage : function() {

					$('.btn-remove').on('click', function(e) {
						$('input').val('');
					});

					$(document).find('form').bt_validate();

					$('select[name="oilTypeId"]').select2({
						theme : "classic",
						placeholder : '请选择',
						minimumResultsForSearch : -1,
						allowClear : true,
					});

				},

				initEmployee : function() {

					var choseEmployees = {
						url : function() {
							var url = window.__CTX__.root + '/employees/query';
							return url + "?companyIds="
									+ $("input[name='organizationId']").val();
						},
						tableColumns : [ '员工代码', '员工名称' ],
						tableValues : [ 'code', 'name' ],
						displayValues : [ 'code', 'name' ],
						values : [ 'id' ]
					}

					$('[data-target="#modal-search-eId"]').modalsearch(
							choseEmployees);
					$('[data-target="#modal-search-fId"]').modalsearch(
							choseEmployees);
					$('[data-target="#modal-search-gId"]').modalsearch(
							choseEmployees);
					$('[data-target="#modal-search-hId"]').modalsearch(
							choseEmployees);

					var options = {
						time : 1000,
						url : window.__CTX__.root + "/employees/query",
						queryName : "code",
						value : [ "id" ],
						display : [ "code", "name" ]
					}

					$('.eId-search').koalaSearch(options);
					$('.fId-search').koalaSearch(options);
					$('.gId-search').koalaSearch(options);
					$('.hId-search').koalaSearch(options);
				},

				submit : function(data) {

					var that = this;

					$('form')
							.shiftform(
									{

										beforeSubmit : function(el) {

											var validate = true;

											// 2. validate driver, main_driver
											// cannot be same as
											// first_vice_driver
											var mainDriver = $(
													'input[name="mainDriver"]')
													.val().trim();
											var firstViceDriver = $(
													'input[name="firstViceDriver"]')
													.val().trim();

											if (mainDriver != null
													&& mainDriver != undefined
													&& mainDriver != ""
													&& mainDriver === firstViceDriver) {
												$(
														'input[name="firstViceDriver"]')
														.show_err_tooltip(
																'请不要重复选择驾驶员！');
												validate = false;
											}

											// 3. validate full_load_number
											var actualLoadNumber = data.actualLoadNumber;
											var inputLoadNumber = $(
													'input[name="fullLoadNumber"]')
													.val().trim();
											if (parseInt(actualLoadNumber) > parseInt(inputLoadNumber)) {
												$(
														'input[name="fullLoadNumber"]')
														.show_err_tooltip(
																'实际装载数量已大于装载能力！');
												validate = false;
											}

											if (validate === false) {
												return false;
											}

											$('form').trigger(
													"validate.ajax.submit");
											return $.bt_validate.result[$.bt_validate.form_id];

										}

									});

				},

			}
		})